/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */
$(document).ready(function () {
    document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
        WeixinJSBridge.call('hideToolbar');
    });
    var $box = $('#input_parking');
    var $message = $('#message');
    var $parkingNo = $('#parking_no');

    // get current location.

    $.get(p.ctx + 'mobileweb/navigation/current/' + p.mid, function (rst) {
        if (rst.status == 'OK') {
            var current = rst.data['stop_val'];
            $('#park_stop').val(current);
            $('#stop_space_no').html(current);
            $('#current').val('');
        } else {
            $('#park_stop').val('');
            $('#current').val('');
        }
    });


    var message = {
        'info': function (message) {
            var n = noty({
                text        : message,
                type        : 'warning',
                dismissQueue: true,
                timeout     : 1000,
                layout      : 'topCenter',
                theme       : 'defaultTheme',
                maxVisible  : 10
            });
        },
        'warn': function (message) {
            var n = noty({
                text        : message,
                type        : 'warning',
                dismissQueue: true,
                timeout     : 1000,
                layout      : 'topCenter',
                theme       : 'defaultTheme',
                maxVisible  : 10
            });
        }
    };

    var box_show = {
        show     : function () {
            if ($box.is(':hidden')) {
                $box.slideDown(400);
            }
        },
        hide     : function () {
            if ($box.is(':visible')) {
                $box.slideUp(400);
            }
        },
        isVisible: function () {
            return $box.is(':visible');
        }
    };

    var box_message = {
        hideMessage: function () {
            $message.html('');
            $message.hide();
        }
    };
    $('.tabTit').click(function () {
        $(this).addClass('on').siblings('.tabTit').removeClass('on');

    });
    $('#peer_floor').on('click', function () {
        $('#floor_type').val('0');
    });

    $('#b2_floor').on('click', function () {
        $('#floor_type').val('1');
    });

    $('#way_direct').on('click', function (e) {
        e.preventDefault();
        var target_input = $('#park_stop').val();
        var parking_no = $('#current').val();
        if (parking_no && target_input) {
            $('#parking_form').submit();
        } else {
            message.warn("请输入停车位置或者当前位置！");
        }
    });

    $('#clean_space').on('click', function (e) {
        e.preventDefault();
        var url = $(this).attr('data-url');
        $.ajax({
            url    : url,
            type   : 'GET',
            cache  : false,
            success: function (rst) {
                if (rst['status'] == "OK") {
                    $('#stop_space_no,#current_space_no').html('&nbsp;');
                    $('#park_stop,#current').val('');
                    message.info('清除位置成功!');
                } else {
                    message.warn('清除位置失败!');
                }
            }
        })
    });

    $('#stop_space, #stop_space_no').on('click', function () {
        if (box_show.isVisible()) {
            message.warn("请先输入当前位置");
            return;
        }
        $('#target_input').val('#stop_space_no,#park_stop');

        box_show.show();
    });
    $('#cancel').click(function () {
        $parkingNo.val('');
        box_message.hideMessage();
        box_show.hide();
    });

    $('#current_space, #current_space_no').on('click', function () {
        if (box_show.isVisible()) {
            message.warn("请先输入停车位置");
            return;
        }
        $('#target_input').val('#current_space_no,#current');
        box_show.show();
    });

    $('#ok_btn').on('click', function () {
        var target_input = $('#target_input').val();
        var parking_no = $('#parking_no').val();
        if (parking_no) {
            if (parking_no.length > 4) {
                message.warn("停车位号码不能超过4位！");
                $parkingNo.focus();
                return;
            }
            var url = $(this).attr('data-url');
            $.post(url,
                {'floor_type': $('#floor_type').val(), 'traget': parking_no, 'mid': p.mid},
                function (rst) {
                    if (rst.status == 'OK') {
                        $(target_input).html(parking_no);
                        $(target_input).val(parking_no);
                        box_message.hideMessage();
                        box_show.hide();
                        $parkingNo.val('');
                    } else {
                        //   tip.show(rst.message)
                        message.warn(rst.message);
                    }
                });
        } else {
            message.warn("请输入停车位！");
            $parkingNo.focus();
        }
    });
});